package com.company.mongo;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;


/**
 * Created by ruoan on 2020/9/28 10:51
 *
 *
 *  链接mongodb
 *  1.ServerAddress集合
 *  2.MongoCredential集合
 *  3.将两个集合作为MongoClient构造器参数传入,获取链接
 *  4.通过链接获取db
 *  5.通过db获取collection
 *  6.通过collection进行CURD操作
 *
 */

@Slf4j
public class MongoMain {

    //private static final Logger logger = LoggerFactory.getLogger(MongoMain.class);
    public static void main(String[] args) {

        //MongoDatabase db = MongoFactory.getDS("192.168.237.128", 27017).getDb("ox");


        List<ServerAddress> adds = new ArrayList<>();
        adds.add(new ServerAddress("192.168.237.128", 27017));


        MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("admin", "admin", "123456".toCharArray());
        ArrayList<MongoCredential> mongoCredentials = CollUtil.toList(mongoCredential);

        MongoClient client = new MongoClient(adds,mongoCredentials);
        //连接到数据库

        MongoDatabase ox = client.getDatabase("test");


        MongoCollection<DBObject> ox1 = ox.getCollection("test",DBObject.class);
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 11);
        map.put("gender","女");
        Document document = new Document(map);
        //ox1.insertOne(document);

        String json = " {" +
                " 'school_code' : '111111', " +
                " 'school_name' : '汉东政法大学', " +
                " 'teacher_idcard' : '0000001', " +
                " 'teacher_name' : '高育良' " +
                " } ";


        DBObject bson = (DBObject)JSON.parse(json);
        System.out.println(bson);
        //ox1.insertOne(bson);
        FindIterable<DBObject> dbObjects = ox1.find();
        System.out.println("dbObjects = " + dbObjects);
        ObjectMapper objectMapper = new ObjectMapper();
        //禁止瞎几把报错
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        dbObjects.forEach(new Consumer<DBObject>() {
            @Override
            public void accept(DBObject dbObject) {
                GreedyOfficial greedyOfficial = null;
                try {
                    greedyOfficial = objectMapper.readValue(dbObject.toString(), GreedyOfficial.class);
                    if (greedyOfficial != null) {
                        log.info(greedyOfficial.toString());
                    }

                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }

            }
        });






    }
}


